/*
 * Cs1555PrjView.java
 */
package cs1555prj;

import java.awt.Component;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

/**
 * The application's main frame.
 */
public class Cs1555PrjView extends FrameView {

    public Cs1555PrjView(SingleFrameApplication app, MyDataBase mdb) {
        super(app);
        initComponents();
        db = mdb;  //create the database obj
        if(!db.openConnection()) {  //make connections
            System.out.println("Fail to create database!");
        }
        customerUI = new CustomerMainFrame(db);
        adminUI = new AdminMainFrame(db);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Cs1555PrjApp.getApplication().getMainFrame();
            aboutBox = new Cs1555PrjAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Cs1555PrjApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        edUser = new javax.swing.JTextField();
        labelWelcom = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        radioCustomer = new javax.swing.JRadioButton();
        radioAdmin = new javax.swing.JRadioButton();
        btnLogin = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        edPassword = new javax.swing.JPasswordField();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cs1555prj.Cs1555PrjApp.class).getContext().getResourceMap(Cs1555PrjView.class);
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        edUser.setText(resourceMap.getString("edUser.text")); // NOI18N
        edUser.setName("edUser"); // NOI18N

        labelWelcom.setFont(resourceMap.getFont("labelWelcom.font")); // NOI18N
        labelWelcom.setText(resourceMap.getString("labelWelcom.text")); // NOI18N
        labelWelcom.setName("labelWelcom"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        radioCustomer.setSelected(true);
        radioCustomer.setText(resourceMap.getString("radioCustomer.text")); // NOI18N
        radioCustomer.setName("radioCustomer"); // NOI18N
        radioCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioCustomerActionPerformed(evt);
            }
        });

        radioAdmin.setText(resourceMap.getString("radioAdmin.text")); // NOI18N
        radioAdmin.setName("radioAdmin"); // NOI18N
        radioAdmin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioAdminActionPerformed(evt);
            }
        });

        btnLogin.setText(resourceMap.getString("btnLogin.text")); // NOI18N
        btnLogin.setName("btnLogin"); // NOI18N
        btnLogin.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnLoginMouseClicked(evt);
            }
        });
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnExit.setText(resourceMap.getString("btnExit.text")); // NOI18N
        btnExit.setName("btnExit"); // NOI18N
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        edPassword.setText(resourceMap.getString("edPassword.text")); // NOI18N
        edPassword.setName("edPassword"); // NOI18N
        edPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                edPasswordKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(119, 119, 119)
                .addComponent(labelWelcom)
                .addContainerGap(94, Short.MAX_VALUE))
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(edPassword)
                    .addComponent(edUser, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE))
                .addContainerGap(81, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addContainerGap(58, Short.MAX_VALUE)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(radioCustomer)
                        .addGap(18, 18, 18)
                        .addComponent(radioAdmin)
                        .addGap(41, 41, 41))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(53, 53, 53)
                        .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(71, 71, 71))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(74, 74, 74)
                .addComponent(labelWelcom)
                .addGap(27, 27, 27)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edUser, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(edPassword)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(radioCustomer)
                    .addComponent(radioAdmin))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                    .addComponent(btnLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE))
                .addContainerGap())
        );

        setComponent(mainPanel);
    }// </editor-fold>//GEN-END:initComponents

    private void radioCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioCustomerActionPerformed
        radioAdmin.setSelected(false);
        radioCustomer.setSelected(true);
        bCustomer = true;
    }//GEN-LAST:event_radioCustomerActionPerformed

    private void radioAdminActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioAdminActionPerformed
        radioAdmin.setSelected(true);
        radioCustomer.setSelected(false);
        bCustomer = false;
    }//GEN-LAST:event_radioAdminActionPerformed

    private void btnLoginMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnLoginMouseClicked
        
    }//GEN-LAST:event_btnLoginMouseClicked

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        //db.closeConnection();
        getFrame().dispose();
        System.exit(0);
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        String user = edUser.getText();
        String password = new String(edPassword.getPassword());
        if (bCustomer) {//customer login
            if (db.isValidCustomer(user, password)) {//if user is valid
                customerUI = new CustomerMainFrame(db);
                customerUI.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
                customerUI.setLocationRelativeTo(this.getFrame());
                customerUI.setUser(user);
                customerUI.setVisible(true);
                getFrame().setVisible(false);
            } else {
                JOptionPane.showMessageDialog(null,
                        "The user name and password you enter is not correct!\nPlease Enter Again!");
            }
        } else {//admin login
            if (db.isValidAdmin(user, password.toString())) {//if administrator is easy
                adminUI = new AdminMainFrame(db);
                adminUI.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
                adminUI.setLocationRelativeTo(this.getFrame());

                adminUI.setVisible(true);
                getFrame().setVisible(false);
            } else {
                JOptionPane.showMessageDialog(null,
                        "The user name and password you enter is not correct!\nPlease Enter Again!");
            }
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void edPasswordKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_edPasswordKeyTyped
        char ch = evt.getKeyChar();
        if(ch == '\n') {//if user enter an enter
            btnLoginActionPerformed(null );
        }
    }//GEN-LAST:event_edPasswordKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnLogin;
    private javax.swing.JPasswordField edPassword;
    private javax.swing.JTextField edUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel labelWelcom;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JRadioButton radioAdmin;
    private javax.swing.JRadioButton radioCustomer;
    // End of variables declaration//GEN-END:variables
    private boolean bCustomer = true;
    private JDialog aboutBox;
    private CustomerMainFrame customerUI;
    private AdminMainFrame adminUI;
    private MyDataBase db;
}
